package com.vgemv.jsutilitysdk.share;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import androidx.core.content.FileProvider;

import com.vgemv.jsutilitysdk.Application.JsBaseApplication;
import com.vgemv.jsutilitysdk.activity.jsloglistactivity.JsLogDetailActivity;
import com.vgemv.jsutilitysdk.file.JsFileManager;
import com.vgemv.jsutilitysdk.log.JsLogManager;

import java.io.File;

public class JsShareManager {
    /**
     * applicationId必须的,因为包含了
     * @param context
     * @param file
     */
    public static void shareFileViaSystem(Context context, File file) {
            final Uri uri;
            int apiVersion = android.os.Build.VERSION.SDK_INT;
            if (apiVersion >= 24) {
                //若SDK大于等于24  获取uri采用共享文件模式
                //Environment.getExternalStorageDirectory().getPath());
                //如果没有为这个文件进行配置,就得不到uri.
                String packageName = context.getPackageName();
                String authorities = String.format("%s.jsutilitysdk.fileprovider",packageName);
                JsLogManager.logfc_v(null,"authorities is: %s",authorities);
                uri = FileProvider.getUriForFile(context.getApplicationContext(), authorities, file);
            } else {
                uri = Uri.fromFile(file);
            }
            Intent share = new Intent(Intent.ACTION_SEND);
            share.putExtra(Intent.EXTRA_STREAM, uri);
            //此处可发送多种文件,会弹出可以处理的app来接受.
            /*"application/pdf"*/
            String str_type = JsFileManager.getMIMEType(file);
            share.setType(str_type);
            share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            share.addCategory(Intent.CATEGORY_DEFAULT);
//        share.setPackage("com.tencent.mobileqq");
//若需要分享到微信，只需要更改Package即可
//share.setPackage("com.tencent.mm")
            if (share.resolveActivity(context.getPackageManager()) != null) {
                context.startActivity(share);
            } else {
//            ToastUtils.show("没有可以处理该pdf文件的应用");
            }
    }
}
